/********************************************************************************************
 Class Name: NMRCreateAccOpptyShare
 Author: Accenture
 Date: 05/22/2012
 Requirement/Project Name: NMR
 Requirement Description: Class for creating and deleting Account and Opportunity Share records.
 ***********************************************************************************************/
 
public class NMRCreateAccOpptyShare{
    //Sharing for Lead method
    public static void createShare(Map<Id,Id> createshareMap,Map<Id,Id> deleteshareMap){
            list<accountShare> accountShareInsert = new list<accountShare>();  // List of accountshares to be created 
            list<accountShare> accountShareDelete = new list<accountShare>(); // List of accountshares to be deleted.
            //Retrieving accountshare recs for deleteshareMap accountids.
        for(accountShare accshare:[Select Id from accountShare where accountid in: deleteshareMap.keyset() and RowCause != 'Owner']){
            accountShareDelete.add(accshare);
        }
        //creating new account shares.
        for(Id accId:createshareMap.keyset()){
                accountShare accShare = new accountShare();
                accShare.accountid = accId; //Account Record being shared
                accShare.UserOrGroupId = createshareMap.get(accId); //Secondary record owner
                accShare.AccountAccessLevel = 'Edit'; //Access
                accShare.OpportunityAccessLevel = 'Edit';
                accShare.CaseAccessLevel = 'Read';   
                accountShareInsert.add(accShare);
        }
        //Insert when accountShareInsert have records.
        if(accountShareInsert.size()>0){
            try{
                insert accountShareInsert;
            }
            catch(DMLException e){
                system.debug('DML Exception thrown :'+e);
            }
        }
        //Delete when accountShareDelete have records.
       if(accountShareDelete.size()>0){
            try{
                delete accountShareDelete;
            }
            catch(DMLException ex){
                system.debug('DML Delete Exception thrown :'+ex);
            }
        }
    }
    //Sharing for Opportunity method
    public static void createShareOppty(Map<Id,Id> createshareMap,Map<Id,Id> deleteshareMap){
            list<OpportunityShare> opptyShareInsert = new list<OpportunityShare>();// List of opportunityshares to be created
            list<OpportunityShare> opptyShareDelete = new list<OpportunityShare>();// List of opportunityshares to be deleted.

    //Retrieving opportunityshare recs for deleteshareMap opptyIds.
        for(OpportunityShare opptyshare:[Select Id from OpportunityShare where OpportunityId in: deleteshareMap.keyset() and RowCause != 'Owner']){
            opptyShareDelete.add(opptyshare);
        }
    //Retrieving OpportunityShare id from createshareMap.
        for(Id opptyId:createshareMap.keyset()){
                OpportunityShare opptyShare = new OpportunityShare();
                opptyShare.opportunityId = opptyId; //Opportunity Record being shared
                opptyShare.UserOrGroupId = createshareMap.get(opptyId); //Secondary record owner
                opptyShare.OpportunityAccessLevel = 'Edit'; //Access  
                opptyShareInsert.add(opptyShare);
        }
        //Insert when opptyShareInsert have records.
         if(opptyShareInsert.size()>0){
            try{
                insert opptyShareInsert;
            }
            catch(DMLException e){
                system.debug('DML Exception thrown :'+e);
            }
        }
        //Delete when opptyShareInsert have records.
         if(opptyShareDelete.size()>0){
            try{
                delete opptyShareDelete;
            }
            catch(DMLException ex){
                system.debug('DML Delete Exception thrown :'+ex);
            }
        }
    }
    
  /**   
  * Test Method    
  */
  
    
  @isTest(SeeAllData=true)
  static void NMRCreateAccOpptyShareTest()
  {
  profile profid = [Select Id from profile q where profile.Name ='NMR Sales'];        
  User u1 = new User(alias = 'Opt21', email='sysadminOpty21@accenture.com',emailencodingkey='UTF-8', lastname='Test Opty 21', languagelocalekey='en_US',localesidkey='en_US', profileid = profid.id,timezonesidkey='America/Los_Angeles', username='testOppty21@testorg.com',SFA_Office__c='NYC',IsActive = True,css_office_name__c = 'NYC',ATTUID__c = 'aaa121');                      
  insert u1;
  User u2 = new User(alias = 'Opt22', email='sysadminOpty22@accenture.com',emailencodingkey='UTF-8', lastname='Test Opty 22', languagelocalekey='en_US',localesidkey='en_US', profileid = profid.id,timezonesidkey='America/Los_Angeles', username='testOppty22@testorg.com',SFA_Office__c='NYC',IsActive = True,css_office_name__c = 'NYC',ATTUID__c = 'aaa122');                      
 insert u2;
 User u3 = new User(alias = 'Opt23', email='sysadminOpty23@accenture.com',emailencodingkey='UTF-8', lastname='Test Opty 23', languagelocalekey='en_US',localesidkey='en_US', profileid = profid.id,timezonesidkey='America/Los_Angeles', username='testOppty23@testorg.com',SFA_Office__c='NYC',IsActive = True,css_office_name__c = 'NYC',ATTUID__c = 'aaa123');                      
 insert u3;

 Id nmrAccountRecordTypeID = RecordTypeHelper.GetRecordTypeID('Account', 'CSS Customer');
 Id nmrOptyRecordTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'NMR Opportunity');

  test.startTest();
  
  Account acc= new Account();
      acc.OOR_Secondary_Account_Owner__c =u1.Id;
      acc.Name = 'Test1';         
      acc.OwnerId = u2.Id;        
      acc.Phone = '9556665559';
      acc.RecordTypeID = nmrAccountRecordTypeID;
      insert acc;
      acc.OOR_Secondary_Account_Owner__c= u3.Id;
      update acc;

      
  Opportunity oppty= new Opportunity();
  oppty.Name = 'Check';         
  oppty.OwnerId = u2.Id; 
  oppty.Secondary_Owner__c =u1.Id;        
  oppty.AccountId = acc.Id;          
  oppty.StageName = 'Stage 1 - Proposal Created';           
  oppty.CloseDate = System.Today();         
  oppty.RecordTypeId = nmrOptyRecordTypeID;         
  insert oppty;
  oppty.Secondary_Owner__c= u3.Id;
  update oppty;


      
  
   
  test.stopTest();
 }

}